package com.boot.security.server.service.impl.mffl.MfflUser;

import com.boot.security.server.dao.SysSmsDao;
import com.boot.security.server.dao.UserDao;
import com.boot.security.server.dao.mffl.*;
import com.boot.security.server.model.SysUser;
import com.boot.security.server.model.mffl.*;
import com.boot.security.server.model.mffl.Vo.LawyerUser;
import com.boot.security.server.model.mffl.Vo.MfflProfitVo;
import com.boot.security.server.page.table.PageTableHandler;
import com.boot.security.server.page.table.PageTableRequest;
import com.boot.security.server.page.table.PageTableResponse;
import com.boot.security.server.service.UserService;
import com.boot.security.server.utils.UserUtil;
import com.boot.security.server.utils.mffl.ReturnBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 用户管理
 */
@Service
public class mfflUserService {

    /**
     * 服务表
     */
    @Autowired
    private MfflServiceDao mfflServiceDao;

    /**
     * 服务类型表
     */
    @Autowired
    private MfflTypeDao mfflTypeDao;

    /**
     *自定义用户表
     */
    @Autowired
    private MfflUserDao mfflUserDao;

    /**
     * 系统用户表
     */
    @Autowired
    private SysSmsDao syssmsDao;

    /**
     *律师收费表
     */
    @Autowired
    MfflLwyerPersonalServiceDao MfflLwyerPersonalServiceDao;

    /**
     *支入支出表
     */
    @Autowired
    private MfflProfitDao mfflProfitDao;


    /**查找过审的律师,个人服务收费情况
     * @param request 分页信息
     * @param serviceid 服务类型
     * @return
     */
    public ReturnBean getServiceLawyer(PageTableRequest request,Integer serviceid){
        ReturnBean returnBean = new ReturnBean();
        //查询律师条件
        HashMap<String, Object> map = new HashMap<>();
        map.put("lawyerinspect",1);
        map.put("deleteState",1);
        map.put("identity",1);
        request.setParams(map);

        PageTableResponse handle = new PageTableHandler(new PageTableHandler.CountHandler() {
            @Override
            public int count(PageTableRequest request) {
                return mfflUserDao.listLaywerCount(request.getParams());
            }
        }, new PageTableHandler.ListHandler() {

            @Override
            public List<mfflUserSimple> list(PageTableRequest request) {
                return mfflUserDao.listLaywer(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);

        //设置律师金额
        List<mfflUserSimple> list = (List<mfflUserSimple>)handle.getData();
        for(int i = 0; i < list.size();i++){
            MfflLwyerPersonalService lawyerServiceMoney = MfflLwyerPersonalServiceDao.getLawyerServiceMoney(list.get(i).getId().intValue(), 3);
            if(lawyerServiceMoney == null){
                list.remove(i);
            }else{
                list.get(i).setLwyerPersonalServiceMoney(lawyerServiceMoney.getMoney());
            }
            //专业领域
            String[] split = list.get(i).getField().split(",");
            List<String> FieldList = new ArrayList<>();
            for(int j = 0; j <split.length;j++){
                MfflType mfflyype = mfflTypeDao.getById(Long.parseLong(split[j]));
                if(mfflyype != null){
                    FieldList.add(mfflyype.getName());
                }
            }
            list.get(i).setFieldList(FieldList);
        }

        returnBean.setDatas(handle);
        return returnBean;
    }


    /**
     * 获取个人支入支出信息
     * @return
     */
    public ReturnBean<MfflProfit> getUserProfitP(PageTableRequest request){
        ReturnBean returnBean = new ReturnBean();

        //获取当前用户,支入支出表
        SysUser sysUser = UserUtil.getLoginUser();
        request.getParams().put("userId",sysUser.getId());

        PageTableResponse handle = new PageTableHandler(new PageTableHandler.CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return mfflProfitDao.count(request.getParams());
            }
        }, new PageTableHandler.ListHandler() {

            @Override
            public List<MfflProfitVo> list(PageTableRequest request) {
                return mfflProfitDao.list(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
        returnBean.setDatas(handle);
        return returnBean;

    }




     /*   HashMap<String, Object> params = new HashMap<>();
        params.put("identity",1);
        params.put("delete_state",1);
        params.put("lawyer_inspect",1);

        List<MfflUser> list = mfflUserDao.list(params, offset, limit);

        returnBean.setDatas(lawyerList);

        return returnBean;*/

}
